import React, { useEffect, useState } from 'react'
import TopNav from '../components/cc-news/TopNav'
import { getNewsCategoryList, getNewsList } from '../components/cc-news/api/newsService'
import '../components/cc-news/css/news.less'

export default function News() {
    // 分类列表
    const [categoryList, setCategoryList] = useState<any>([])
    // 新闻列表
    const [newsList, setNewsList] = useState([])

    // 获取/设置分类列表
    async function getCategoryList() {
        const res = await getNewsCategoryList({})
        // console.log(res);
        setCategoryList(res.data.data)
    }

    // 获取/设置分类列表
    async function getList(params: any) {
        const res = await getNewsList(params)
        // console.log(res);
        setNewsList(res.data.rows)
    }

    useEffect(() => {
        getCategoryList()
    }, [])

    useEffect(() => {
        if (categoryList[0]?.id) {
            getList({
                type: categoryList[0]?.id
            })
        }
    }, [categoryList])

    return (
        <div className='news'>
            <TopNav categoryList={categoryList} newsList={newsList} getList={getList}></TopNav>
        </div>
    )
}
